package com.website.control
{
	import com.website.event.SWFAddressEvent;
	import com.website.util.StringUtil;
	
	import flash.errors.*;
	import flash.events.*;
	import flash.external.*;
	import flash.net.*;
	import flash.system.*;
	import flash.utils.*;
	
	public class PathManager extends Object
	{
		
		public static var pathNames:Array = new Array();
		public static var clickPathNames:Array = new Array();
		
		public static var onInit:Function;
		private static var _initializer:Boolean = _initialize();
		private static var _availability:Boolean = ExternalInterface.available;
		private static var _timer:Timer = null;
		private static var _dispatcher:EventDispatcher = new EventDispatcher();
		private static var _strict:Boolean = true;
		private static var _value:String = "";
		public static var onChange:Function;
		private static var _init:Boolean = false;
		private static var _initChange:Boolean = false;
		
		public function PathManager()
		{
			throw new IllegalOperationError("PathManager cannot be instantiated.");
		}
		
		public static function updateAddress(str:String):void
		{
			trace(str);
			setValue(str);
		}
		
		private static function _strictCheck(param1:String, param2:Boolean) : String
		{
			if (PathManager.getStrict())
			{
				if (param2)
				{
					if (param1.substr(0, 1) != "/")
					{
						param1 = "/" + param1;
					}
				}
				else if (param1 == "")
				{
					param1 = "/";
				}
			}
			return param1;
		}
		
		public static function getTracker() : String
		{
			return _availability ? (ExternalInterface.call("SWFAddress.getTracker") as String) : ("");
		}
		
		public static function hasEventListener(param1:String) : Boolean
		{
			return _dispatcher.hasEventListener(param1);
		}
		
		private static function _check(event:TimerEvent) : void
		{
			if ((typeof(PathManager["onInit"]) == "function" || _dispatcher.hasEventListener("init")) && !_init)
			{
				PathManager._setValueInit(_getValue());
				PathManager._init = true;
			}
			if (typeof(PathManager["onChange"]) == "function" || _dispatcher.hasEventListener("change"))
			{
//				_timer.stop();
				PathManager._init = true;
				PathManager._setValueInit(_getValue());
			}
			return;
		}
		
		private static function _setValue(param1:String) : void
		{
			if (param1 == "undefined" || param1 == null)
			{
				param1 = "";
			}
			if (PathManager._value == param1 && PathManager._init)
			{
				return;
			}
			if (!PathManager._initChange)
			{
				return;
			}
			PathManager._value = param1;
			if (!_init)
			{
				PathManager._init = true;
				if (typeof(PathManager["onInit"]) == "function" || _dispatcher.hasEventListener("init"))
				{
					_dispatchEvent(SWFAddressEvent.INIT);
				}
			}
			_dispatchEvent(SWFAddressEvent.CHANGE);
			return;
		}
		
		public static function getParameterNames() : Array
		{
			var _loc_1:String = null;
			var _loc_2:Number = NaN;
			var _loc_3:Array = null;
			var _loc_4:Array = null;
			var _loc_5:Number = NaN;
			_loc_1 = PathManager.getValue();
			_loc_2 = _loc_1.indexOf("?");
			_loc_3 = new Array();
			if (_loc_2 != -1)
			{
				_loc_1 = _loc_1.substr((_loc_2 + 1));
				if (_loc_1 != "" && _loc_1.indexOf("=") != -1)
				{
					_loc_4 = _loc_1.split("&");
					_loc_5 = 0;
					while (_loc_5 < _loc_4.length)
					{
						
						_loc_3.push(_loc_4[_loc_5].split("=")[0]);
						_loc_5 = _loc_5 + 1;
					}
				}
			}
			return _loc_3;
		}
		
		public static function setTitle(param1:String) : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.setTitle", param1);
			}
			return;
		}
		
		private static function _getValue() : String
		{
			var _loc_1:String = null;
			var _loc_2:String = null;
			var _loc_3:Array = null;
			_loc_2 = null;
			if (_availability)
			{
				_loc_1 = ExternalInterface.call("SWFAddress.getValue") as String;
				_loc_3 = ExternalInterface.call("SWFAddress.getIds") as Array;
				if (_loc_3 != null)
				{
					_loc_2 = _loc_3.toString();
				}
			}
			if (_loc_2 == null || !_availability)
			{
				_loc_1 = PathManager._value;
			}
			else if (_loc_1 == "undefined" || _loc_1 == null)
			{
				_loc_1 = "";
			}
			return _strictCheck(_loc_1 || "", false);
		}
		
		public static function up() : void
		{
			var _loc_1:String = null;
			_loc_1 = PathManager.getPath();
			PathManager.setValue(_loc_1.substr(0, _loc_1.lastIndexOf("/", _loc_1.length - 2) + (_loc_1.substr((_loc_1.length - 1)) == "/" ? (1) : (0))));
			return;
		}
		
		public static function resetStatus() : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.resetStatus");
			}
			return;
		}
		
		public static function getBaseURL() : String
		{
			var _loc_1:String = null;
			_loc_1 = null;
			if (_availability)
			{
				_loc_1 = String(ExternalInterface.call("SWFAddress.getBaseURL"));
			}
			return _loc_1 == null || _loc_1 == "null" || !_availability ? ("") : (_loc_1);
		}
		
		public static function getTitle() : String
		{
			var _loc_1:String = null;
			_loc_1 = _availability ? (ExternalInterface.call("SWFAddress.getTitle") as String) : ("");
			if (_loc_1 == "undefined" || _loc_1 == null)
			{
				_loc_1 = "";
			}
			return _loc_1;
		}
		
		public static function getPath() : String
		{
			var _loc_1:String = null;
			_loc_1 = PathManager.getValue();
			if (_loc_1.indexOf("?") != -1)
			{
				return _loc_1.split("?")[0];
			}
			return _loc_1;
		}
		
		public static function href(param1:String, param2:String = "_self") : void
		{
			if (_availability && Capabilities.playerType == "ActiveX")
			{
				ExternalInterface.call("SWFAddress.href", param1, param2);
				return;
			}
			navigateToURL(new URLRequest(param1), param2);
			return;
		}
		
		public static function popup(param1:String, param2:String = "popup", param3:String = "\"\"", param4:String = "") : void
		{
			if (_availability && (Capabilities.playerType == "ActiveX" || ExternalInterface.call("asual.util.Browser.isSafari")))
			{
				ExternalInterface.call("SWFAddress.popup", param1, param2, param3, param4);
				return;
			}
			navigateToURL(new URLRequest("javascript:popup=window.open(\"" + param1 + "\",\"" + param2 + "\"," + param3 + ");" + param4 + ";void(0);"), "_self");
			return;
		}
		
		private static function _initialize() : Boolean
		{
			if (_availability)
			{
				ExternalInterface.addCallback("getSWFAddressValue", function () : String
				{
					return _value;
				}
				);
				ExternalInterface.addCallback("setSWFAddressValue", _setValue);
			}
			if (_timer == null)
			{
				_timer = new Timer(10);
				_timer.addEventListener(TimerEvent.TIMER, _check);
			}
//			_timer.start();
			return true;
		}
		
		public static function setValue(param1:String) : void
		{
			if (param1 == "undefined" || param1 == null)
			{
				param1 = "";
			}
			param1 = _strictCheck(param1, true);
			if (PathManager._value == param1)
			{
				return;
			}
			PathManager._value = param1;
			if (_availability && PathManager._init)
			{
				ExternalInterface.call("SWFAddress.setValue", param1);
			}
			_dispatchEvent(SWFAddressEvent.CHANGE);
			return;
		}
		
		public static function getValue() : String
		{
			return _strictCheck(_value || "", false);
		}
		
		private static function _setValueInit(param1:String) : void
		{
			PathManager._value = param1;
			if (!_init)
			{
				_dispatchEvent(SWFAddressEvent.INIT);
			}
			else
			{
				_dispatchEvent(SWFAddressEvent.CHANGE);
			}
			_initChange = true;
			return;
		}
		
		public static function getStrict() : Boolean
		{
			var _loc_1:String = null;
			_loc_1 = null;
			if (_availability)
			{
				_loc_1 = ExternalInterface.call("SWFAddress.getStrict") as String;
			}
			return _loc_1 == null ? (_strict) : (_loc_1 == "true");
		}
		
		public static function dispatchEvent(event:Event) : Boolean
		{
			return _dispatcher.dispatchEvent(event);
		}
		
		private static function _dispatchEvent(param1:String) : void
		{
			if (_dispatcher.hasEventListener(param1))
			{
				_dispatcher.dispatchEvent(new SWFAddressEvent(param1));
			}
			param1 = param1.substr(0, 1).toUpperCase() + param1.substring(1);
			if (typeof(PathManager["on" + param1]) == "function")
			{
				var _loc_2:* = PathManager;
				_loc_2.PathManager["on" + param1]();
			}
			return;
		}
		
		public static function getQueryString() : String
		{
			var _loc_1:String = null;
			var _loc_2:Number = NaN;
			_loc_1 = PathManager.getValue();
			_loc_2 = _loc_1.indexOf("?");
			if (_loc_2 != -1 && _loc_2 < _loc_1.length)
			{
				return _loc_1.substr((_loc_2 + 1));
			}
			return "";
		}
		
		public static function forward() : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.forward");
			}
			return;
		}
		
		public static function removeEventListener(param1:String, param2:Function) : void
		{
			_dispatcher.removeEventListener(param1, param2, false);
			return;
		}
		
		public static function setStrict(param1:Boolean) : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.setStrict", param1);
			}
			_strict = param1;
			return;
		}
		
		public static function setHistory(param1:Boolean) : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.setHistory", param1);
			}
			return;
		}
		
		public static function addEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
		{
			_dispatcher.addEventListener(param1, param2, param3, param4, param5);
			return;
		}
		
		public static function getHistory() : Boolean
		{
			return _availability ? (ExternalInterface.call("SWFAddress.getHistory") as Boolean) : (false);
		}
		
		public static function back() : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.back");
			}
			return;
		}
		
		public static function getStatus() : String
		{
			var _loc_1:String = null;
			_loc_1 = _availability ? (ExternalInterface.call("SWFAddress.getStatus") as String) : ("");
			if (_loc_1 == "undefined" || _loc_1 == null)
			{
				_loc_1 = "";
			}
			return _loc_1;
		}
		
		public static function getParameter(param1:String) : String
		{
			var _loc_2:String = null;
			var _loc_3:Number = NaN;
			var _loc_4:Array = null;
			var _loc_5:Array = null;
			var _loc_6:Number = NaN;
			_loc_2 = PathManager.getValue();
			_loc_3 = _loc_2.indexOf("?");
			if (_loc_3 != -1)
			{
				_loc_2 = _loc_2.substr((_loc_3 + 1));
				_loc_4 = _loc_2.split("&");
				_loc_6 = _loc_4.length;
				while (_loc_6--)
				{
					
					_loc_5 = _loc_4[_loc_6].split("=");
					if (_loc_5[0] == param1)
					{
						return _loc_5[1];
					}
				}
			}
			return "";
		}
		
		public static function setStatus(param1:String) : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.setStatus", param1);
			}
			return;
		}
		
		public static function go(param1:int) : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.go", param1);
			}
			return;
		}
		
		public static function getPathNames() : Array
		{
			var _loc_1:String = null;
			var _loc_2:Array = null;
			_loc_1 = PathManager.getPath();
			_loc_2 = _loc_1.split("/");
			if (_loc_1.substr(0, 1) == "/" || _loc_1.length == 0)
			{
				_loc_2.splice(0, 1);
			}
			if (_loc_1.substr((_loc_1.length - 1), 1) == "/")
			{
				_loc_2.splice((_loc_2.length - 1), 1);
			}
			return _loc_2;
		}
		
		public static function setTracker(param1:String) : void
		{
			if (_availability)
			{
				ExternalInterface.call("SWFAddress.setTracker", param1);
			}
			return;
		}
		
		public static function updateMenuArray(param1:String, param2:String) : void
		{
			switch(param1)
			{
				case "tier0":
				{
					pathNames.length = 2;
					pathNames[1] = StringUtil.toLowercase(param2);
					break;
				}
				case "tier1":
				{
					pathNames.length = 3;
					pathNames[2] = StringUtil.toLowercase(param2);
					break;
				}
				case "tier2":
				{
					pathNames.length = 4;
					pathNames[3] = StringUtil.toLowercase(param2);
					break;
				}
				case "tier3":
				{
					pathNames.length = 5;
					pathNames[4] = StringUtil.toLowercase(param2);
					break;
				}
				case "news":
				{
					pathNames.length = 1;
					pathNames[0] = StringUtil.toLowercase(param2);
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}
	}
}
